﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.Xml.Linq
Imports GenDotNet.Task.Common
Imports GenDotNet.TemplateSupport.Strings
Imports GenDotNet.ObjectModel
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">

Namespace VB
   Public MustInherit Class DotNetLanguageBase
      Inherits DotNetBase

      Protected Sub New()
         MyBase.New()
      End Sub

      Public Overrides Function CleanOutput(ByVal code As String) As String
         Return code.CleanOutput()
      End Function

#Region "Namespace dependent redirects"
      Public Overrides Function ClassOutputClose(ByVal info As TypeInfo) As String
         Return info.OutputClose
      End Function

      Public Overrides Function ClassOutputOpening(ByVal info As TypeInfo) As String
         Return info.OutputOpening()
      End Function

      Public Overrides Function CommentOutput(ByVal info As CommentInfo) As String
         Return info.Output()
      End Function

      Public Overrides Function ConstructorOutput(ByVal info As ConstructorInfo) As String
         Return info.Output()
      End Function

      Public Overrides Function FieldOutput(ByVal info As VariableInfo) As String
         Return info.Output()
      End Function

      Public Overrides Function FileImportsOutput(ByVal fileImports As ImportInfoCollection) As String
         Return fileImports.Output()
      End Function

      Public Overrides Function FileNamespaceOutput(ByVal info As NamespaceInfo) As String
         Return info.Output()
      End Function

      Public Overrides Function FunctionOutput(ByVal info As FunctionInfo) As String
         Return info.Output()
      End Function

      Public Overrides Function GenericCollectionOutput(ByVal infoCollection As GenericInfoCollection) As String
         Return infoCollection.Output()
      End Function

      Public Overrides Function GenericOutput(ByVal info As GenericInfo) As String
         Return info.Output()
      End Function

      Public Overrides Function PropertyOutput(ByVal info As PropertyInfo) As String
         Return info.Output()
      End Function

      Public Overrides Function RegionOutput(ByVal info As RegionInfo) As String
         Return info.Output()
      End Function

      Public Overrides Function SubOutput(ByVal info As SubInfo) As String
         Return info.Output()
      End Function

      Public Overrides Function VariableOutput(ByVal info As VariableInfo) As String
         Return info.Output()
      End Function

      Protected Overrides Function FileOpening() As String
         Return _
<code>
Option Strict On
Option Explicit On
Option Infer On

</code>.Value
      End Function

#End Region

      Public Overrides Function OutputFileName(ByVal partialName As String) As String
         Return partialName & ".vb"
      End Function

      Public Overrides Sub SetStrings()
         Strings.wrapString = " _" & Strings.nl
         Strings.returnString = "Return"
         Strings.newString = "New"
         Strings.andAlsoString = "AndAlso"
         Strings.andString = "And"
         Strings.orElseString = "OrElse"
         Strings.orString = "Or"
         Strings.meString = "Me"
         Strings.trueString = "True"
         Strings.falseString = "False"
         Strings.notString = "Not"
         Strings.nullString = "Nothing"
         Strings.throwString = "Throw"
         Strings.baseString = "MyBase"
         Strings.getTypeString = "GetType"
         Strings.semiColonString = ""
      End Sub

      Public Overrides Function BitwiseCombine(ByVal var As String, ByVal literal As UInt64) As String
         ' VB does less implicit conversions on bitwise compares with literals
         Return BitwiseCombine(var, literal.ToString(), StandardNetType.uInt32)
      End Function

      Public Overrides Function BitwiseCombine(ByVal var As String, ByVal literal As Int64) As String
         ' VB does less implicit conversions on bitwise compares with literals
         Return BitwiseCombine(var, literal.ToString(), StandardNetType.Int64)
      End Function

      Public Overloads Function BitwiseCombine(ByVal var As String, ByVal literal As UInt32) As String
         ' VB does less implicit conversions on bitwise compares with literals
         Return BitwiseCombine(var, literal.ToString(), StandardNetType.uInt32)
      End Function

      Public Overloads Function BitwiseCombine(ByVal var As String, ByVal literal As Int32) As String
         ' VB does less implicit conversions on bitwise compares with literals
         Return BitwiseCombine(var, literal.ToString(), StandardNetType.Int32)
      End Function

      Private Overloads Function BitwiseCombine(ByVal var As String, ByVal literal As String, ByVal nettype As StandardNetType) As String
         Return _
<code><%= var %> Or CType(<%= literal %>, <%= nettype.Output %>)</code>.Value
      End Function

      Public Overrides Function BitwiseContains(ByVal var As String, ByVal literal As UInt64) As String
         Return BitwiseContains(var, literal.ToString, StandardNetType.Uint64)
      End Function

      Public Overrides Function BitwiseContains(ByVal var As String, ByVal literal As Int64) As String
         Return BitwiseContains(var, literal.ToString, StandardNetType.Uint64)
      End Function

      Public Overloads Function BitwiseContains(ByVal var As String, ByVal literal As UInt32) As String
         Return BitwiseContains(var, literal.ToString, StandardNetType.UInt32)
      End Function

      Public Overloads Function BitwiseContains(ByVal var As String, ByVal literal As Int32) As String
         Return BitwiseContains(var, literal.ToString, StandardNetType.Int32)
      End Function

      Private Overloads Function BitwiseContains(ByVal var As String, ByVal literal As String, ByVal nettype As StandardNetType) As String
         Return _
         <code>(<%= var %> And CType(<%= literal %>, <%= nettype.Output %>)) = CType(<%= literal %>, <%= nettype.Output %>)</code>.Value
      End Function

      Public Overloads Overrides Function WithSemiColons(ByVal code As IEnumerable(Of System.Xml.Linq.XElement)) As String
         Return code.Value
      End Function

      Public Overloads Overrides Function WithSemiColons(ByVal code As XElement) As String
         Return code.Value
      End Function

      Public Overloads Overrides Function WithSemiColons(ByVal code As String) As String
         Return code
      End Function

      Protected Overrides Sub _DoNotDeriveFromDotNetBaseDeriveFromDotNetLanguageBase()
         ' Dummy code
      End Sub

      '      Public Sub SetStrings( _
      '               ByVal base As DotNetBase)

      '         Strings.wrapString = " _" & Strings.crlf
      '         Strings.returnString = "Return"
      '         Strings.newString = "New"
      '         Strings.andAlsoString = "AndAlso"
      '         Strings.andString = "And"
      '         Strings.orElseString = "OrElse"
      '         Strings.orString = "Or"
      '         Strings.meString = "Me"
      '         Strings.trueString = "True"
      '         Strings.falseString = "False"
      '         Strings.notString = "Not"
      '         Strings.nullString = "Nothing"
      '         Strings.throwString = "Throw"
      '         Strings.baseString = "MyBase"
      '         Strings.getTypeString = "GetType"
      '         Strings.semiColonString = ""

      '      End Sub

      '      Public Function OutputFileName(ByVal partialName As String) As String
      '         Return partialName & ".vb"
      '      End Function

      '      Public Function OutputFile(ByVal base As DotNetBase) As String
      '         Return _
      '   <code>
      '      <%= base.FileImports.Output %>
      '      <%= If(base.FileNamespace Is Nothing, base.OutputContents(), base.FileNamespace.Output()) %>
      '   </code>.Value
      '      End Function

      '      Public Function CleanOutput(ByVal out As String) As String
      '         Return out.Replace("[", "(").Replace("]", ")")
      '      End Function

      '      Public Function WithSemiColons(ByVal code As IEnumerable(Of XElement)) As String
      '         Return code.Value
      '      End Function

      '      Public Function WithSemiColons(ByVal code As XElement) As String
      '         Return code.Value
      '      End Function

      '#Region "Output Convenience Functions - This region should remain identical in C# and VB to facilitate copying!"

      '      Public Function OutputClass( _
      '               ByVal template As DotNetBase, _
      '               ByVal classInfo As TypeInfo, _
      '               ByVal del As EmptyDelegate) _
      '               As String
      '         Return _
      '<code>
      '   <%= template.PushContext(classInfo) %>
      '   <%= classInfo.OutputOpening() %>
      '   <%= del.Invoke() %>
      '   <%= classInfo.OutputClose() %>
      '   <%= template.PopContext() %>
      '</code>.Value
      '      End Function

      '      Public Function OutputRegion( _
      '               ByVal template As DotNetBase, _
      '               ByVal name As String, _
      '               ByVal del As EmptyDelegate) _
      '               As String
      '         Return New RegionInfo(name, del).Output
      '      End Function

      '      Public Function OutputComment( _
      '               ByVal template As DotNetBase, _
      '               ByVal comment As String) _
      '               As String
      '         Return New CommentInfo(comment).Output
      '      End Function

      '      Public Function OutputXmlComment( _
      '               ByVal template As DotNetBase, _
      '               ByVal comment As String) _
      '               As String
      '         Return New CommentInfo(comment, True).Output
      '      End Function

      '      Public Function OutputConstructor( _
      '               ByVal template As DotNetBase, _
      '               ByVal scope As Scope, _
      '               ByVal del As EmptyDelegate, _
      '               ByVal ParamArray parameters() As ParameterInfo) _
      '               As String
      '         Return New ConstructorInfo(template.CurrentTypeInfo, scope, del, parameters).Output()
      '      End Function

      '      Public Function OutputSub( _
      '              ByVal template As DotNetBase, _
      '              ByVal name As String, _
      '              ByVal scope As Scope, _
      '              ByVal modifiers As MemberModifiers, _
      '              ByVal del As EmptyDelegate, _
      '              ByVal ParamArray parameters() As ParameterInfo) _
      '              As String
      '         Return New SubInfo(name, template.CurrentTypeInfo, scope, modifiers, del, parameters).Output()
      '      End Function

      '      Public Function OutputFunction( _
      '              ByVal template As DotNetBase, _
      '              ByVal name As String, _
      '              ByVal scope As Scope, _
      '              ByVal modifiers As MemberModifiers, _
      '              ByVal returnType As NetTypeInfo, _
      '              ByVal del As EmptyDelegate, _
      '              ByVal ParamArray parameters() As ParameterInfo) _
      '              As String
      '         Return New FunctionInfo(name, template.CurrentTypeInfo, scope, modifiers, returnType, del, parameters).Output()
      '      End Function


      '      Public Function OutputFunctionCall( _
      '               ByVal functionName As String, _
      '               ByVal generics As GenericInfoCollection, _
      '               ByVal ParamArray params() As ParameterInfo) _
      '               As String
      '         Return _
      '   <code>
      '      <%= functionName.WithSpace %><%= generics.Output %>()<%= nws %>
      '   </code>.Value
      '      End Function

      '      Public Function OutputFunctionCall( _
      '               ByVal functionName As String, _
      '               ByVal generic As String, _
      '               ByVal ParamArray params() As ParameterInfo) _
      '               As String
      '         Return _
      '   <code>
      '      <%= functionName.WithSpace %><%= (New GenericInfo(generic)).Output %>()<%= nws %>
      '   </code>.Value
      '      End Function

      '      Public Function OutputProperty( _
      '               ByVal template As DotNetBase, _
      '               ByVal name As String, _
      '               ByVal scope As Scope, _
      '               ByVal modifiers As MemberModifiers, _
      '               ByVal returnType As NetTypeInfo, _
      '               ByVal isReadOnly As Boolean, _
      '               ByVal getDelegate As EmptyDelegate, _
      '               ByVal setDelegate As EmptyDelegate, _
      '               ByVal ParamArray parameters() As ParameterInfo) _
      '               As String
      '         Return New PropertyInfo(name, template.CurrentTypeInfo, scope, modifiers, _
      '                   returnType, isReadOnly, getDelegate, setDelegate, parameters).Output(template.ObjectData)
      '      End Function


      '      Public Function OutputProperty( _
      '               ByVal template As DotNetBase, _
      '               ByVal prop As IPropertyData, _
      '               ByVal scope As Scope, _
      '               ByVal modifiers As MemberModifiers, _
      '               ByVal isReadOnly As Boolean, _
      '               ByVal getDelegate As EmptyDelegate, _
      '               ByVal setDelegate As EmptyDelegate, _
      '               ByVal ParamArray parameters() As ParameterInfo) _
      '               As String
      '         Return New PropertyInfo(prop.Name, template.CurrentTypeInfo, scope, modifiers, _
      '                   prop.NetType, isReadOnly, getDelegate, setDelegate, parameters).Output(template.ObjectData)
      '      End Function

      '      Public Function OutputVariable( _
      '               ByVal template As DotNetBase, _
      '               ByVal name As String, _
      '               ByVal netType As NetTypeInfo) _
      '               As String
      '         Return New VariableInfo(name, True, netType).Output
      '      End Function

      '      Public Function OutputVariable( _
      '               ByVal template As DotNetBase, _
      '               ByVal name As String, _
      '               ByVal netType As NetTypeInfo, _
      '               ByVal setNew As Boolean) _
      '               As String
      '         Return New VariableInfo(name, True, netType, setNew).Output
      '      End Function

      '      Public Function OutputVariable( _
      '               ByVal template As DotNetBase, _
      '               ByVal name As String, _
      '               ByVal netType As NetTypeInfo, _
      '               ByVal defaultValue As String) _
      '               As String
      '         ' Assume private for fields
      '         Return New VariableInfo(name, True, netType, defaultValue).Output
      '      End Function

      '      Public Function OutputField( _
      '               ByVal template As DotNetBase, _
      '               ByVal name As String, _
      '               ByVal netType As NetTypeInfo) _
      '               As String
      '         Return New VariableInfo(name, Scope.Private, netType).Output
      '      End Function

      '      Public Function OutputField( _
      '               ByVal template As DotNetBase, _
      '               ByVal name As String, _
      '               ByVal netType As NetTypeInfo, _
      '               ByVal setNew As Boolean) _
      '               As String
      '         Return New VariableInfo(name, Scope.Private, netType, setNew).Output
      '      End Function

      '      Public Function OutputField( _
      '               ByVal template As DotNetBase, _
      '               ByVal name As String, _
      '               ByVal netType As NetTypeInfo, _
      '               ByVal defaultValue As String) _
      '               As String
      '         ' Assume private for fields
      '         Return New VariableInfo(name, Scope.Private, netType, defaultValue).Output
      '      End Function

      '      Public Function OutputField( _
      '         ByVal prop As IPropertyData) _
      '         As String
      '         ' Assume private for fields
      '         Return New VariableInfo(prop.Name, Scope.Private, prop.NetType, prop.DefaultValue).Output
      '      End Function

      '      Public Function OutputField( _
      '         ByVal child As IChildData) _
      '         As String
      '         ' Assume private for fields
      '         Return New VariableInfo(child.Name, Scope.Private, StandardNetType.Unknown, True).Output
      '      End Function

      '#End Region

   End Class

   Public Module LanguageExtensions
      <Runtime.CompilerServices.Extension()> _
      Public Function OutputField( _
              ByVal prop As IPropertyData) _
              As String
         ' Assume private for fields
         Return New VariableInfo(prop.Name, Scope.Private, prop.NetType, prop.DefaultValue).Output
      End Function

      <Runtime.CompilerServices.Extension()> _
      Public Function OutputField( _
         ByVal child As IChildData) _
         As String
         ' Assume private for fields
         Return New VariableInfo(child.Name, Scope.Private, StandardNetType.Unknown, True).Output
      End Function

      <Runtime.CompilerServices.Extension()> _
      Public Function CleanOutput(ByVal out As String) As String
         Return out.Replace("[", "(").Replace("]", ")")
      End Function

   End Module
End Namespace

